/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package medicamentos;

import java.awt.Image;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Locale;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import java.lang.String;
import javax.swing.table.DefaultTableCellRenderer;
/**
 *
 * @author MAYRA
 */
public class Medicamentos extends javax.swing.JFrame {

    DefaultTableModel model;
       java.text.SimpleDateFormat dt = new java.text.SimpleDateFormat("yyyy-MM-dd");
 

    public Medicamentos() {
        initComponents();
        desactivar();
        btnNuevo.setEnabled(true);
       txtStock.setEnabled(false);
          this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        cargarTablaMedicamentos("","");
  
        tMedicamentos.addMouseListener(new java.awt.event.MouseAdapter(){
                public void mouseClicked(java.awt.event.MouseEvent e){
                    try{
                        if((tMedicamentos.getSelectedColumn()==0) && (tMedicamentos.getSelectedRow()!=-1)){
                            int fila=tMedicamentos.getSelectedRow();
                            txtCodigo.setText(tMedicamentos.getValueAt(fila,2).toString().trim());
                            txtNomMedicina.setText(tMedicamentos.getValueAt(fila,3).toString().trim());
                            txtMedPresentacion.setText(tMedicamentos.getValueAt(fila,4).toString().trim());
                            txtFecElabora.setDate(Date.valueOf(tMedicamentos.getValueAt(fila,5).toString().trim()));
                            txtFechaVence.setDate(Date.valueOf(tMedicamentos.getValueAt(fila,6).toString().trim()));
                            txtPrecioCompra.setText(tMedicamentos.getValueAt(fila,7).toString().trim());
                           txtPrecioVenta.setText(tMedicamentos.getValueAt(fila,8).toString().trim());
                           txtStock.setText(tMedicamentos.getValueAt(fila,9).toString().trim());
                            activar();                    
                            btnCancelar.setEnabled(true);
                            txtCodigo.setEnabled(false);
                            //txtPrecioCompra.setEnabled(false);
                             // txtPrecioVenta.setEnabled(false);
                             }else
                            if((tMedicamentos.getSelectedColumn()==1) && (tMedicamentos.getSelectedRow()!=-1)){
                                int fila=tMedicamentos.getSelectedRow();
                            txtCodigo.setText(tMedicamentos.getValueAt(fila,2).toString().trim());
                            txtNomMedicina.setText(tMedicamentos.getValueAt(fila,3).toString().trim());
                            txtMedPresentacion.setText(tMedicamentos.getValueAt(fila,4).toString().trim());
                            txtFecElabora.setDate(Date.valueOf(tMedicamentos.getValueAt(fila,5).toString().trim()));
                            txtFechaVence.setDate(Date.valueOf(tMedicamentos.getValueAt(fila,6).toString().trim()));
                           txtPrecioCompra.setText(tMedicamentos.getValueAt(fila,7).toString().trim());
                            txtPrecioVenta.setText(tMedicamentos.getValueAt(fila,8).toString().trim());
                    
                           txtStock.setText(tMedicamentos.getValueAt(fila,9).toString().trim());
                            
                            desactivar();
                            //lbFoto.setEnabled(false);
                                borrar();
                            }
                    }catch(Exception ex){
                        JOptionPane.showMessageDialog(null, "Error"+ex);
                    }
                }
            });
    } 
    public void guardarMedicamentos()
            
    {
        if(txtCodigo.getText().trim().isEmpty())
        {
            JOptionPane.showMessageDialog(null, "No se puede ingresar un medicamento sin código");            
            txtCodigo.requestFocus();
        }
        else{
          if(txtNomMedicina.getText().trim().isEmpty()){
              JOptionPane.showMessageDialog(null, "No se puede ingresar un medicamento sin nombre");
            txtNomMedicina.requestFocus();
              
          }
           if(txtMedPresentacion.getText().trim().isEmpty()){
              JOptionPane.showMessageDialog(null, "No se puede ingresar un medicamento sin Nombre de Presentacion");
            txtMedPresentacion.requestFocus();   
          }
           else
           {
                        
            if(txtPrecioCompra.getText().trim().isEmpty()){
              JOptionPane.showMessageDialog(null, "Ingrese el valor de la compra");
            txtPrecioCompra.requestFocus();
                        }
             else{
                             
            if(txtPrecioVenta.getText().trim().isEmpty()){
              JOptionPane.showMessageDialog(null, "Ingrese el valor de la venta");
            txtPrecioVenta.requestFocus();
                        }
        
        else{
                           
       Conexions cc= new Conexions();
                Connection cn = cc.conectar();
                double med_p_uni_compra,med_p_uni_venta;
               int med_stock;
                String med_codigo,med_nombre,med_presentacion,med_fec_ven,med_fec_ela;
                String sql="";
                med_codigo=txtCodigo.getText();
                med_nombre=txtNomMedicina.getText();
                med_presentacion=txtMedPresentacion.getText();
                med_p_uni_compra=Double.parseDouble(txtPrecioCompra.getText());
                 med_p_uni_venta=Double.parseDouble(txtPrecioVenta.getText());
                  med_stock=Integer.parseInt(txtStock.getText());
                 
                 
           java.util.Date fecha1 = txtFecElabora.getDate();
            String fecha = dt.format(fecha1); 
           med_fec_ela=fecha; 
            
           java.util.Date fecha2 = txtFechaVence.getDate();
            String fechas = dt.format(fecha2); 
            med_fec_ven=fechas;
                                          
              sql="insert into medicamentos(MED_CODIGO,MED_NOMBRE,MED_PRESENTACION,MED_FEC_VEN,MED_FEC_ELA,MED_P_UNI_COMPRA,MED_P_UNI_VENTA,MED_STOCK) values(?,?,?,?,?,?,?,?)";
         try {
                PreparedStatement psw= cn.prepareStatement(sql);
                psw.setString(3, med_codigo);
                psw.setString(4, med_nombre );
                psw.setString(5,med_presentacion );
                 psw.setDate(6,Date.valueOf(fecha));
                 psw.setDate(7,Date.valueOf(fechas));
                 psw.setDouble(8,med_p_uni_compra);
                 psw.setDouble(9, med_p_uni_venta);
              psw.setInt(10,med_stock);
                int n= psw.executeUpdate();// el execute Update devuelve un numero entero debuelve cero si no insertas nada y uno si se inserto
                if(n>0)//si  se inserto
                {
                JOptionPane.showMessageDialog(null, "Se grabo correctamente");
                   cargarTablaMedicamentos("","");
                  limpiar();
                  btnNuevo.setEnabled(true);
                  btnGuardar.setEnabled(true);
                }
                
        } catch (SQLException ex) {
            
            JOptionPane.showMessageDialog(null, "No se grabo...." +ex);
       } 
       }
       }
      }
    }
        }
    
    public void limpiar(){
          txtCodigo.setText("");
        txtNomMedicina.setText("");
        txtMedPresentacion.setText("");
        txtPrecioCompra.setText("");
        txtPrecioVenta.setText("");
          txtFecElabora.setDate(null);
        txtFechaVence.setDate(null);
        //txtStock.setText("");
           }
        public void activar(){
           txtCodigo.setEnabled(true);
        txtNomMedicina.setEnabled(true);
        txtMedPresentacion.setEnabled(true);
        txtPrecioCompra.setEnabled(true);
        txtPrecioVenta.setEnabled(true);
          txtFecElabora.setEnabled(true);
        txtFechaVence.setEnabled(true);
       // txtStock.setEnabled(true);
       
    }
    public void desactivar(){
        txtCodigo.setEnabled(false);
        txtNomMedicina.setEnabled(false);
        txtMedPresentacion.setEnabled(false);
        txtPrecioCompra.setEnabled(false);
        txtPrecioVenta.setEnabled(false);
          txtFecElabora.setEnabled(false);
        txtFechaVence.setEnabled(false);
        txtStock.setEnabled(false);
    }
      public void activarBoton(){
        btnNuevo.setEnabled(true);
        btnCancelar.setEnabled(true);
        btnGuardar.setEnabled(true);
        
    }
     public void desactivarBoton(){
        btnNuevo.setEnabled(false);
        btnCancelar.setEnabled(false);
        btnGuardar.setEnabled(false);
          } 
    public void cargarTablaMedicamentos(String codigo,String nombre){
        String[] titulos ={" "," ","CODIGO","NOMBRE","PRESENTACION","FECHA VENCE","FECHA ELABORACION","PRECIO UNITARIO DE COMPRA","PRECIO UNITARIO DE VENTA","STOCK"};
        String []registros=new String[10];
        String sql;
        Conexions cc=new Conexions();
        Connection cn=cc.conectar();
        
        sql="select *from medicamentos Where  med_codigo LIKE '%" +codigo+"%' and med_nombre LIKE '" +nombre+"%'order by med_codigo,med_nombre";
        model=new DefaultTableModel(null,titulos); 
        
        try {
            Statement st=cn.createStatement();
            ResultSet rs=st.executeQuery(sql);
             
            while(rs.next()){
                registros[2]=rs.getString("MED_CODIGO");
                registros[3]=rs.getString("MED_NOMBRE");
                registros[4]=rs.getString("MED_PRESENTACION");
                registros[5]=rs.getString("MED_FEC_VEN");
                registros[6]=rs.getString("MED_FEC_ELA");
                registros[7]=rs.getString("MED_P_UNI_COMPRA");
                registros[8]=rs.getString("MED_P_UNI_VENTA");
                registros[9]=rs.getString("MED_STOCK");
              
                
                
                model.addRow(registros);
            }
            tMedicamentos.setModel(model);
            tamaniotabla();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,"LA TABLA TIENE PROBLEMAS AL CARGARSE"+ex);
        }
        
        }
    
    public void actualizar(){
        Conexions cc=new Conexions();
        Connection cn=cc.conectar();
        String sql="",med_fec_ven,med_fec_ela;
            java.util.Date fecha1 = txtFecElabora.getDate();
            String fecha = dt.format(fecha1); 
            med_fec_ven=fecha;
            
            
            java.util.Date fecha2 = txtFechaVence.getDate();
            String fechas = dt.format(fecha2); 
            med_fec_ela=fechas;
          sql="update medicamentos SET med_nombre='"+txtNomMedicina.getText()+"',med_presentacion='"+txtMedPresentacion.getText()+
                 "',med_fec_ven='"+med_fec_ven+"'"+",med_fec_ela='"+med_fec_ela+"' ,med_p_uni_compra='"+
                 txtPrecioCompra.getText()+ "', med_p_uni_venta='"+
                 txtPrecioVenta.getText()+ "',med_stock='"+txtStock.getText()+"' where med_codigo='"+txtCodigo.getText()+"'";
        
       try {
            PreparedStatement psw =cn.prepareStatement(sql);
            int n=psw.executeUpdate();
            if(n>0){
                JOptionPane.showMessageDialog(null, "Se actualizo correctamente");
                 
		cargarTablaMedicamentos("","");
                     limpiar();
              
                }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,"No se actualizo" +ex);
        }
        
    }
    private void actualizarTabla() {
          Conexions cc=new Conexions();
        Connection cn=cc.conectar();
         int fila=tMedicamentos.getSelectedRow();
        String sql="";
        if (fila!=-1) {
                    sql="UPDATE MEDICAMENTOS "
                + "SET MED_NOMBRE='"+tMedicamentos.getValueAt(fila,3).toString().trim().toUpperCase()+"',"
                + "MED_PRESENTACION='"+tMedicamentos.getValueAt(fila,4).toString().trim().toUpperCase()+"',"
                + "MED_FEC_VEN='"+tMedicamentos.getValueAt(fila,5).toString().trim()+"',"
                + "MED_FEC_ELA='"+tMedicamentos.getValueAt(fila,6).toString().trim()+"',"
                + "MED_P_UNI_COMPRA='"+tMedicamentos.getValueAt(fila,7)+"'," 
                + "MED_P_UNI_VENTA='"+tMedicamentos.getValueAt(fila,8)+"',"  
                + "MED_STOCK='"+tMedicamentos.getValueAt(fila,9).toString().trim()+"'"  
                 + "WHERE MED_CODIGO='"+tMedicamentos.getValueAt(fila, 2).toString()+"'";
        
        try {
            PreparedStatement psw=cn.prepareStatement(sql);
            int n=psw.executeUpdate();
            
            if (n>0) {
                JOptionPane.showMessageDialog(this, "Actualizacion correcta");
                cargarTablaMedicamentos("","");
                limpiar();
                desactivar();
 
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error en la Actualizacion"+e);
        }
        }
    }
    public void  borrar(){
         if(JOptionPane.showConfirmDialog(new JInternalFrame(),
          "Esta seguro de Eliminar el regidtro","Borrar registro",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){
 
       Conexions cc=new Conexions();
        Connection cn=cc.conectar();
        String sql="";
        sql="delete from medicamentos where med_codigo='"+txtCodigo.getText()+"'";
                try{
                  PreparedStatement psw=cn.prepareStatement(sql);
                  int n=psw.executeUpdate();
                  if(n>0)
                  {
                      JOptionPane.showMessageDialog(null,"Se elimino correctamente");
                      cargarTablaMedicamentos("","");
                      limpiar();
                  }
                    
                }catch(SQLException ex){
                    JOptionPane.showMessageDialog(null,"NO se elimino");
                }
    }
         
    }
    
    public void tamaniotabla(){
        Icon img = new javax.swing.ImageIcon("src\\imagenes\\b_edit.png");
        Icon img2 = new javax.swing.ImageIcon("src\\imagenes\\b_drop.png");
        DefaultTableCellRenderer renderer1 = new javax.swing.table.DefaultTableCellRenderer();
        DefaultTableCellRenderer renderer = new javax.swing.table.DefaultTableCellRenderer();
        renderer1.setIcon(img);
        tMedicamentos.getColumnModel().getColumn(0).setCellRenderer(renderer1);
        renderer.setIcon(img2);
        tMedicamentos.getColumnModel().getColumn(1).setCellRenderer(renderer);
        //columna 0
         tMedicamentos.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(20); 
         tMedicamentos.getTableHeader().getColumnModel().getColumn(0).setMinWidth(20);
         //columna 1
         tMedicamentos.getTableHeader().getColumnModel().getColumn(1).setMaxWidth(20); 
         tMedicamentos.getTableHeader().getColumnModel().getColumn(1).setMinWidth(20);
         //columna 2
         tMedicamentos.getTableHeader().getColumnModel().getColumn(2).setMaxWidth(60); 
         tMedicamentos.getTableHeader().getColumnModel().getColumn(2).setMinWidth(60);
         //columna 3
         tMedicamentos.getTableHeader().getColumnModel().getColumn(3).setMaxWidth(90); 
         tMedicamentos.getTableHeader().getColumnModel().getColumn(3).setMinWidth(90);
         //columna 4
         tMedicamentos.getTableHeader().getColumnModel().getColumn(4).setMaxWidth(90); 
         tMedicamentos.getTableHeader().getColumnModel().getColumn(4).setMinWidth(90);
         //columna 5
         tMedicamentos.getTableHeader().getColumnModel().getColumn(5).setMaxWidth(90); 
         tMedicamentos.getTableHeader().getColumnModel().getColumn(5).setMinWidth(90);
         //columna 6
         tMedicamentos.getTableHeader().getColumnModel().getColumn(6).setMaxWidth(80); 
         tMedicamentos.getTableHeader().getColumnModel().getColumn(6).setMinWidth(80);
         //columna 7
         tMedicamentos.getTableHeader().getColumnModel().getColumn(7).setMaxWidth(70); 
         tMedicamentos.getTableHeader().getColumnModel().getColumn(7).setMinWidth(70);
         //columna 8
         tMedicamentos.getTableHeader().getColumnModel().getColumn(8).setMaxWidth(70); 
         tMedicamentos.getTableHeader().getColumnModel().getColumn(8).setMinWidth(70);
         //columna 9
          tMedicamentos.getTableHeader().getColumnModel().getColumn(9).setMaxWidth(80); 
          tMedicamentos.getTableHeader().getColumnModel().getColumn(9).setMinWidth(80);
         
          }
    
   
    
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tMedicamentos = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        btnNuevo = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtNomMedicina = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtMedPresentacion = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtFechaVence = new com.toedter.calendar.JDateChooser();
        jLabel6 = new javax.swing.JLabel();
        txtFecElabora = new com.toedter.calendar.JDateChooser();
        jLabel8 = new javax.swing.JLabel();
        txtPrecioCompra = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtPrecioVenta = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        BuscarNombre = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtStock = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(153, 244, 248));

        jLabel1.setFont(new java.awt.Font("Andalus", 1, 18));
        jLabel1.setText("MEDICAMENTOS");

        tMedicamentos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tMedicamentos.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tMedicamentosKeyReleased(evt);
            }
        });
        jScrollPane2.setViewportView(tMedicamentos);

        btnNuevo.setFont(new java.awt.Font("Tahoma", 1, 12));
        btnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/nuevo.jpg"))); // NOI18N
        btnNuevo.setText("NUEVO");
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        btnGuardar.setFont(new java.awt.Font("Tahoma", 1, 12));
        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/guardar.jpg"))); // NOI18N
        btnGuardar.setText("GUARDAR");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnCancelar.setFont(new java.awt.Font("Tahoma", 1, 12));
        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/cancelar.jpg"))); // NOI18N
        btnCancelar.setText("CANCELAR");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(btnNuevo, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnGuardar, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
                        .addGap(13, 13, 13))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(13, 13, 13))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnNuevo)
                .addGap(18, 18, 18)
                .addComponent(btnGuardar)
                .addGap(18, 18, 18)
                .addComponent(btnCancelar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel2.setFont(new java.awt.Font("Sylfaen", 1, 12));
        jLabel2.setText("CODIGO");

        txtCodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCodigoActionPerformed(evt);
            }
        });
        txtCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtCodigoKeyTyped(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Sylfaen", 1, 12));
        jLabel3.setText("NOMBRE");

        txtNomMedicina.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNomMedicinaActionPerformed(evt);
            }
        });
        txtNomMedicina.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtNomMedicinaKeyTyped(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Sylfaen", 1, 12));
        jLabel4.setText("MEDICINA PRESENTACION");

        txtMedPresentacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtMedPresentacionActionPerformed(evt);
            }
        });
        txtMedPresentacion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtMedPresentacionKeyTyped(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Sylfaen", 1, 12));
        jLabel7.setText("FECHA ELABORACION");

        txtFechaVence.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtFechaVenceKeyPressed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Sylfaen", 1, 12));
        jLabel6.setText("FECHA VENCE");

        txtFecElabora.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtFecElaboraKeyPressed(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Sylfaen", 1, 12));
        jLabel8.setText("PRECIO UNITARIO DE COMPRA");

        txtPrecioCompra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPrecioCompraActionPerformed(evt);
            }
        });
        txtPrecioCompra.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtPrecioCompraKeyTyped(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Sylfaen", 1, 12));
        jLabel9.setText("PRECIO UNITARIO DE VENTA");

        txtPrecioVenta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPrecioVentaActionPerformed(evt);
            }
        });
        txtPrecioVenta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtPrecioVentaKeyTyped(evt);
            }
        });

        jLabel12.setFont(new java.awt.Font("Sylfaen", 1, 12));
        jLabel12.setForeground(new java.awt.Color(0, 0, 204));
        jLabel12.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/Search.png"))); // NOI18N
        jLabel12.setText("BUSCAR");

        BuscarNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BuscarNombreActionPerformed(evt);
            }
        });
        BuscarNombre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                BuscarNombreKeyTyped(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                BuscarNombreKeyReleased(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Sylfaen", 1, 12));
        jLabel10.setText("STOCK");

        txtStock.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtStockActionPerformed(evt);
            }
        });
        txtStock.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtStockKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(33, 33, 33)
                                .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(10, 10, 10)
                                .addComponent(txtMedPresentacion, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(txtNomMedicina, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtFecElabora, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtFechaVence, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel8))
                            .addComponent(jLabel9)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(txtStock, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtPrecioVenta, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtPrecioCompra, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(79, 79, 79))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel10)
                .addContainerGap())
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(271, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(BuscarNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
                .addGap(271, 271, 271))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtNomMedicina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtMedPresentacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7)
                    .addComponent(txtFechaVence, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addComponent(txtFecElabora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txtPrecioCompra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel9))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(txtPrecioVenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10)
                    .addComponent(txtStock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15)
                .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BuscarNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(16, Short.MAX_VALUE))
        );

        jButton1.setText("ACTUALIZAR");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(255, 255, 255)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(88, 88, 88)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(16, 16, 16))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2)))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(31, 31, 31)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(126, 126, 126)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(15, 15, 15))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        limpiar();
        btnNuevo.requestFocus();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void txtNomMedicinaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNomMedicinaKeyTyped
        // TODO add your handling code here:
       char car = evt.getKeyChar();
        String temp = String.valueOf(car).toUpperCase();
        car = temp.charAt(0);
        evt.setKeyChar(car);
        
        if(txtNomMedicina.getText().length()>=30) 
            evt.consume();
        if((car<'A' || car>'Z')) 
            evt.consume();
      
       
                                       

    }//GEN-LAST:event_txtNomMedicinaKeyTyped

    private void txtCodigoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCodigoKeyTyped
        // TODO add your handling code here:
        txtCodigo.setText(txtCodigo.getText().toUpperCase());
        char car = evt.getKeyChar();
        if(txtCodigo.getText().length()>=5) evt.consume();       
        
    }//GEN-LAST:event_txtCodigoKeyTyped

    private void txtCodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCodigoActionPerformed
        // TODO add your handling code here:
        txtNomMedicina.requestFocus();
    }//GEN-LAST:event_txtCodigoActionPerformed

    private void txtNomMedicinaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNomMedicinaActionPerformed
        // TODO add your handling code here:
        txtMedPresentacion.requestFocus();
    }//GEN-LAST:event_txtNomMedicinaActionPerformed

    private void txtMedPresentacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtMedPresentacionActionPerformed
txtFecElabora.requestFocus();
    }//GEN-LAST:event_txtMedPresentacionActionPerformed

    private void txtPrecioCompraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPrecioCompraActionPerformed
        // TODO add your handling code here:
        txtPrecioVenta.requestFocus();
    }//GEN-LAST:event_txtPrecioCompraActionPerformed

    private void txtPrecioVentaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPrecioVentaActionPerformed
        // TODO add your handling code here:
        txtStock.requestFocus();
    }//GEN-LAST:event_txtPrecioVentaActionPerformed

    private void txtStockActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtStockActionPerformed
        // TODO add your handling code here:
        btnGuardar.requestFocus();
    }//GEN-LAST:event_txtStockActionPerformed

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        // TODO add your handling code here:
   limpiar();
   activar();
   activarBoton();
        txtCodigo.requestFocus();
    }//GEN-LAST:event_btnNuevoActionPerformed

    private void txtMedPresentacionKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMedPresentacionKeyTyped
        // TODO add your handling code here:
              char car = evt.getKeyChar();
        String temp = String.valueOf(car).toUpperCase();
        car = temp.charAt(0);
        evt.setKeyChar(car);
        
        if(txtMedPresentacion.getText().length()>=30) 
            evt.consume();
        if((car<'A' || car>'Z')) 
            evt.consume();
      
       
                                     

    }//GEN-LAST:event_txtMedPresentacionKeyTyped

    private void txtFechaVenceKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtFechaVenceKeyPressed
        // TODO add your handling code here:
          txtFecElabora.setEnabled(true);
        txtFecElabora.requestFocus();
    }//GEN-LAST:event_txtFechaVenceKeyPressed

    private void txtFecElaboraKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtFecElaboraKeyPressed
        // TODO add your handling code here:
          txtPrecioCompra.setEnabled(true);
        txtPrecioCompra.requestFocus();
    }//GEN-LAST:event_txtFecElaboraKeyPressed

    private void txtPrecioCompraKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPrecioCompraKeyTyped
        // TODO add your handling code here:
  
       if(!Character.isDigit(evt.getKeyChar())&&evt.getKeyChar()!='.'){
           evt.consume();
       }
       if(evt.getKeyChar()=='.'&&txtPrecioCompra.getText().contains(".")){
           evt.consume();
       }
                   
    }//GEN-LAST:event_txtPrecioCompraKeyTyped

    private void txtStockKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtStockKeyTyped
        // TODO add your handling code here:
        txtStock.setText(txtStock.getText().toUpperCase());
        char car = evt.getKeyChar();
        if(txtStock.getText().length()>=5) evt.consume();        
       if ((car<'0' || car>'9' ))
           evt.consume();
    }//GEN-LAST:event_txtStockKeyTyped

    private void txtPrecioVentaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPrecioVentaKeyTyped
        // TODO add your handling code here:
        if(!Character.isDigit(evt.getKeyChar())&&evt.getKeyChar()!='.'){
           evt.consume();
       }
       if(evt.getKeyChar()=='.'&&txtPrecioVenta.getText().contains(".")){
           evt.consume();
       }
    }//GEN-LAST:event_txtPrecioVentaKeyTyped

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        // TODO add your handling code here:
        guardarMedicamentos();
        cargarTablaMedicamentos("","");
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void BuscarNombreKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_BuscarNombreKeyTyped
        // TODO add your handling code here:
          cargarTablaMedicamentos("",BuscarNombre.getText().trim());
    }//GEN-LAST:event_BuscarNombreKeyTyped

    private void BuscarNombreKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_BuscarNombreKeyReleased
        // TODO add your handling code here:
         cargarTablaMedicamentos("",BuscarNombre.getText().trim());
    }//GEN-LAST:event_BuscarNombreKeyReleased

    private void tMedicamentosKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tMedicamentosKeyReleased
        // TODO add your handling code here:
   actualizarTabla();
    }//GEN-LAST:event_tMedicamentosKeyReleased

    private void BuscarNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BuscarNombreActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_BuscarNombreActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    actualizar();
}//GEN-LAST:event_jButton1ActionPerformed

        
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Medicamentos().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField BuscarNombre;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable tMedicamentos;
    public static javax.swing.JTextField txtCodigo;
    private com.toedter.calendar.JDateChooser txtFecElabora;
    private com.toedter.calendar.JDateChooser txtFechaVence;
    private javax.swing.JTextField txtMedPresentacion;
    private javax.swing.JTextField txtNomMedicina;
    private javax.swing.JTextField txtPrecioCompra;
    private javax.swing.JTextField txtPrecioVenta;
    private javax.swing.JTextField txtStock;
    // End of variables declaration//GEN-END:variables
}
